Fill 100 random integers in array and count even and odds in array with Java

public class ArrayTest{

	public static int[] createArray(){
		int[]arr = new int[100];
		for(int i = arr.length-1; 0 <= i; i --){
			arr[i] = (int)(Math.random()*100);
		}
		return arr;
	}

	public static void statsDisplay(int[] intArray){
		int evens = 0, odds = 0, sum = 0;
		for(int n: intArray){
			sum += n;
			if(0 == n%2){
				evens ++;
			} else {
				odds ++;
			}
		}
		double avg = sum/intArray.length*1.0;
		System.out.printf("evens: %d, odds: %d, avg: %.2f%n", evens, odds, avg);
	}

	public static void main(String[]args){
		statsDisplay(createArray());
	}
}
Download

Comments

Popular posts from this blog